﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Protocol.player;
using Pixysoft.Weblications.Sango.Protocol.equipment;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Weblications.Sango.Battle;
using Pixysoft.Weblications.Sango.Constant.battle;
using Pixysoft.Weblications.Sango.Protocol;

namespace Pixysoft.Weblications.Sango.Command.section
{
    /// <summary>
    /// 获取属臣、装备
    /// </summary>
    partial class SangoSectionHandler
    {
        public section_fight section_fight(long sectionserialcode, string targetusercode)
        {
            string userCode = SangoManager.Authentication();

            section_fight response = ReflectionManager.CreatePojo<section_fight>();
            response.Succeed = false;

            Entities.Usr_Profile profile = UsrProfileDao.getUsrProfile(userCode);
            Entities.Usr_Activity activity = UsrActivityDao.getHistory(userCode);


            Entities.Usr_Queue queue = UsrQueueDao.UsrQueueStartup(userCode, CtiQueueTypecodeCollection.BATTLE, userCode, BattleFormular.getBattleCooldownTimespan(activity));

            if (queue == null)
            {
                response.Message = "队列冷却中";
                return response;
            }

            if (activity.Freebattlecount <= 0 && !StageFormular.isFreeAttackPlayer(activity.Freebattletypecode))
            {
                response.Message = "无行动力";
                return response;
            }

            //检查地区

            Entities.Usr_Section mysection = UsrSectionDao.GetSection(userCode);
            Entities.Usr_Section targetsection = UsrSectionDao.GetSection(targetusercode);

            if (mysection.Sectiontypecode != targetsection.Sectiontypecode)
            {
                response.Message = "无法攻击不同区域的用户";
                return response;
            }

            if (mysection.Nationtypecode != targetsection.Nationtypecode)
            {
                response.Message = "无法攻击不同国家的用户";
                return response;
            }

            Entities.Usr_Mission mission = MissionDao.getMission(userCode);

            IEntityNoebeTransaction command = SettingHelper.EntityTransaction;

            command.Begin(delegate()
            {

                PvxBattleHistory pvxHistory = getAttackResult();

                if (pvxHistory.BattleResult == BattleResultCollection.ATTACKWIN)
                {
                    if (StageFormular.isFreeAttackPlayer(activity.Freebattletypecode))
                    {
                        activity.Freebattletypecode = (activity.Freebattletypecode - FreeBattleTypecodeCollection.ATTACK_PVP);
                    }
                    else
                    {
                        activity.Usedbattlecount = activity.Usedbattlecount + 1;
                        activity.Freebattlecount = activity.Freebattlecount - 1;
                    }
                }


                bool dropEqm = SectionFormular.isDropEquipment();

                Entities.Usr_Equipment eqm = null;

                if (dropEqm)
                {
                    eqm = EquipmentDao.tryDropUserEquipment(command, profile, SectionFormular.dropEquipment());

                    if (eqm != null)
                    {
                        response.eqm = EquipmentFacade.ToEquipment(eqm, new List<Entities.Usr_Equipmentextra>());
                    }
                }

                command.Update(activity);

                command.Update(queue);

                if (MissionDao.processDailyMission(mission, MissionDailyCollection.ATTACK_PVP))
                {
                    command.Update(mission);
                }

                command.Commit();
            });

            response.profile = UsrProfileFacade.ToProfile(profile);
            response.activity = UsrProfileFacade.ToActivity(profile, activity);
            response.queue = UsrQueueFacade.ToQueue(queue);
            response.Succeed = true;
            return response;
        }


        private static PvxBattleHistory getAttackResult()
        {
            PvxBattleHistory history = new PvxBattleHistory("", -1, -1);
            history.BattleResult = BattleResultCollection.ATTACKWIN;
            return history;
        }
    }

    public interface section_fight : ISangoResponse
    {
        player_profile profile { get;set;}
        player_activity activity { get;set;}
        player_queue queue { get;set;}

        usr_equipment eqm { get;set;}
    }
}
